package com.rop.system.exception;

import com.rop.result.Result;
import com.rop.result.ResultCodeEnum;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * @Title: GlobalExceptionHandler
 * @Author Rop
 * @Date 2023/4/8 21:38
 * @description: 全局异常统一处理
 */

@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 全局异常处理
     * @param e 异常
     * @return 结果
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result<Object> error(Exception e) {
        e.printStackTrace();
        return Result.fail("操作失败，原因：" + e.getMessage());
    }

    /**
     * 自定义异常
     * @param be 自定义异常
     * @return 返回结果
     */
    @ExceptionHandler(BizException.class)
    @ResponseBody
    public Result<Object> error(BizException be) {
        be.printStackTrace();
        return Result.fail(be.getMsg());
    }

    /**
     * spring security异常
     * @return 返回结果
     */
    @ExceptionHandler(AccessDeniedException.class)
    @ResponseBody
    public Result<Object> error() {
        return Result.build(null, ResultCodeEnum.PERMISSION);
    }
}
